<?php
class b2c_ctl_admin_business_order extends desktop_controller{
	public function __construct($app)
    {
        parent::__construct($app);
		header("cache-control: no-store, no-cache, must-revalidate");
    }

	public function index(){
//		$oOrder= app::get('b2c')->model('orders');
//			$business_id=$oOrder->getList('business_id');
//			if($business_id){
//				foreach($business_id as $k=>$v){
//					if($v['business_id']==0){
//					unset($v['business_id']);
//					}else{
//						$filter['business_id'][] = $v['business_id'];
//					}
//				}
//			}
			
		$this->finder('b2c_mdl_business_orders',array(
            'title'=>app::get('b2c')->_('供应商订单'),
            'actions'=>array(),
			'use_buildin_set_tag'=>true,
            'use_buildin_recycle'=>false,
            'use_buildin_filter'=>true,
            'use_view_tab'=>true,
            ));
	}

    /**
     * 供应商订单view 列表
     * @param null
     * @return null
     */
    public function _views(){
        $mdl_delivery = $this->app->model('business_orders');
        $sub_menu = array(
            0=>array('label'=>app::get('b2c')->_('未确认'),'optional'=>false,'filter'=>array('status'=>array('0','2'),'order_status|noequal'=>'dead')),
            1=>array('label'=>app::get('b2c')->_('已确认'),'optional'=>false,'filter'=>array('status'=>'1')),
            2=>array('label'=>app::get('b2c')->_('全部'),'optional'=>false),
        );

        if(isset($_GET['optional_view'])) $sub_menu[$_GET['optional_view']]['optional'] = false;

        foreach($sub_menu as $k=>$v){
            if($v['optional']==false){
                $show_menu[$k] = $v;
                if(is_array($v['filter'])){
                    $v['filter'] = array_merge(array(),$v['filter']);
                }else{
                    $v['filter'] = array();
                }
                $show_menu[$k]['filter'] = $v['filter']?$v['filter']:null;
                if($k==$_GET['view']){
                    $show_menu[$k]['newcount'] = true;
                    $show_menu[$k]['addon'] = $mdl_delivery->count($v['filter']);
                }
                $show_menu[$k]['href'] = 'index.php?app=b2c&ctl=admin_business_order&act=index&view='.($k).(isset($_GET['optional_view'])?'&optional_view='.$_GET['optional_view'].'&view_from=dashboard':'');
            }elseif(($_GET['view_from']=='dashboard')&&$k==$_GET['view']){
                $show_menu[$k] = $v;
            }
        }
        return $show_menu;
    }

    function to_confirm($order_id){
        $obj_bo = app::get('b2c')->model('business_orders');
        $b_order = $obj_bo->getRow('*',array('childOrder_id'=>$order_id));
        $item = $obj_bo->get_order_items($order_id);
        $this->pagedata['order_id'] = $b_order['mainOrder_id'];
        $this->pagedata['b_order_id'] = $order_id;
        $this->pagedata['status'] = $b_order['status'];
        $this->pagedata['order_status'] = $b_order['order_status'];
        $this->pagedata['item']['goods'] = $this->get_glink_info($item);
        $this->singlepage('admin/order/business_confirm.html');
    }

    function get_glink_info(&$data){
        foreach($data as $key => $item){
            $obj_rg = app::get('b2c')->model('business_recommend_goods');
            $filter = array('mainOrder_id'=>$item['mainOrder_id'],'childOrder_id'=>$item['childOrder_id'],'business_id'=>$item['business_id'],'goods_id'=>$item['goods_id']);
            $data[$key]['glink'] = $obj_rg->getRow('*',$filter);
            $data[$key]['glink_filter'] = array('business_id'=>$item['business_id'],'goods_id|noequal'=>$item['goods_id'],'marketable'=>'true');
        }
        return $data;
    }

    function do_confirm(){
        $objOrder = $this->app->model('business_orders_objects');
        $objRg = $this->app->model('business_recommend_goods');
		$orderMdl = $this->app->model('business_orders');

        $this->begin();
        foreach((array)$_POST['confirm_num'] as $k=>$v){
            $order_item = $objOrder->getRow('*',array('obj_id'=>$k));
            if(isset($order_item)){
                $sData['state'] = '1';
                $sData['confirm_num'] = $v;
                $sData['has_confirm_num'] = $v;
                if($_POST['r_goods_id'] && isset($_POST['r_goods_id'][$order_item['goods_id']]) && !empty($_POST['r_goods_id'][$order_item['goods_id']])){
                    $row['childOrder_id'] = $_POST['b_order_id'];
                    $row['mainOrder_id'] = $_POST['order_id'];
                    $row['business_id'] = $order_item['business_id'];
                    $row['goods_id'] = $order_item['goods_id'];
                    $row['r_goods_id'] = $_POST['r_goods_id'][$order_item['goods_id']];
                    $objRg->save($row);
                }
                $objOrder->update($sData,array('obj_id'=>$k));
            }
        }
        $b_status = $this->check_b_orders(array('childOrder_id'=>$_POST['b_order_id']));
		$st = $orderMdl->getRow('status',array('childOrder_id'=>$_POST['b_order_id']));
		$state = $st['status'];
		if($state == 1){
			$this->end(false,app::get('b2c')->_('保存失败！已确认库存商品'));
		}else{
			$orderMdl->update(array('status'=>$b_status),array('childOrder_id'=>$_POST['b_order_id']));

			//如果全部确认了，order单子里的c_status也是确认状态
			$c_status = $this->check_order(array('mainOrder_id'=>$_POST['order_id']));
			if($c_status == 1 || $c_status == 2){
				$orders_mdl = $this->app->model('orders');
				//是否有推荐商品或者确认数量与购数量不同
				$orders_rg_mdl = $this->app->model('business_recommend_goods');
				if($orders_rg_mdl->count(array('mainOrder_id'=>$_POST['order_id'])) || $this->check_item_num(array('mainOrder_id'=>$_POST['order_id']))){
					$c_status = '2';//库存已确认
					$orders_mdl->update(array('c_status'=>$c_status),array('order_id'=>$_POST['order_id']));
				}else{
					$obj_order_update = kernel::single('b2c_order_update');
					$obj_order_update->update_confirm_order(array('mainOrder_id'=>$_POST['order_id']),$_POST['order_id']);
				}
	//            $orders_mdl->update(array('c_status'=>$c_status),array('order_id'=>$_POST['order_id']));

                //供应商库存确认后要给客户发送短信,添加订单总金额，总件数，付款公司---jyy---2016/10/19
                $orders_objects_mdl = $this->app->model('order_objects');
                $orders_objects = $orders_objects_mdl->getList('*',array('order_id'=>$_POST['order_id']));
                $goods_num = "";
                foreach($orders_objects as $k=>$value)
                {
                    $goods_num += intval($value['quantity']);
                }
                $orders = $orders_mdl->dump(array('order_id'=>$_POST['order_id']),'*');

                $obj_account=$this->app->model('member_account');
                $fire_data = array('order_id'=>$_POST['order_id'],
                    'total_amount'=>$orders['total_amount'],//订单总金额
                    'total_number'=>$goods_num,//总件数
                    'pay_account'=>$orders['company_name']);//付款公司
				$obj_account->fireEvent('storeconfirm',$fire_data,$orders['member_id']);
                //app::get('b2c')->model('member_messenger')
                    //->actionSend('orders-storeconfirm',$fire_data,null,$orders['member_id']);
			}
		}
        $this->end(true, app::get('b2c')->_('确认成功'));
    }

    //检查确认数量与购数量
    function check_item_num($filter){
        $objOrder = $this->app->model('business_orders_objects');
        $order_items = $objOrder->getList('*',$filter);
        foreach($order_items as $key => $item){
            if($item['has_confirm_num'] != $item['quantity']){
                $result = true;
                break;
            }
        }
        return $result;
    }
     
    //检验供应商订单明细状态
    function check_b_orders($filter){
        $obj_goods = $this->app->model('business_orders_objects');
        $goods = $obj_goods->getList('*',$filter);
        $status = '0';
        $flg = true;
        foreach($goods as $key => $item){
            if($item['state'] == '1'){
                $status = '2';
            }
            if($item['state'] == '0'){
                $flg = false;
            }
        }
        if($flg){
            $status = '1';
        }
        return $status;
    }

    //检验订单下供应商订单状态
    function check_order($filter){
        $obj_goods = $this->app->model('business_orders');
        $goods = $obj_goods->getList('*',$filter);
        $status = '0';
        $flg = true;
        foreach($goods as $key => $item){
            if($item['status'] == '0'){
                $flg = false;
            }
        }
        if($flg){
            $status = '1';
            $obj_order = $this->app->model('orders');
            $orders = $obj_order->getRow('*',array('order_id'=>$filter['mainOrder_id']));
            $obj_type = $this->app->model('dlytype');
            $dt_info = $obj_type->dump($orders['shipping_id']);
            if($dt_info['cost_confirm'] == 'false'){
                $status = '2';
            }
			if($orders['styke'] == 'Самовывоз' || $orders['styke'] == '自提')
			{
				$status = '2';
			}
        }
        
        return $status;
    }
}